home *** CD-ROM | disk | FTP | other *** search
- /***
- * MirrorLists.c
- *
- * Manage the mirror lists at a high level
- *
- ***/
-
- #include "debug_me.h"
- #include "Exceptions.h"
- #include "debug_me.h"
- #include "URLHelperComponent.h"
- #include "URLHelperComponentPrivate.h"
- #include "MemoryBuffer.h"
- #include "TextUtils.h"
-
- /**
- ** string buffer size is the size of the strings that we will be storing in our own little
- ** private string area.
- **/
-
- #define STRING_BUFFER_SIZE 512
-
- /**
- * _URLHelperNewMirrorList
- *
- * Add a new mirror list to our list of mirrors!
- *
- **/
- pascal ComponentResult _URLHelperNewMirrorList (Handle storage, StringPtr string,
- URLHMirrorListPtr *ref)
- {
- OSErr theErr;
- URLHMirrorListPtr newList;
- URLGlobalsHandle globals = (URLGlobalsHandle) storage;
-
- /**
- ** Check the inputs here...
- **/
-
- theErr = paramErr;
- require (ref != 0 && string != 0, BadParams);
-
- /**
- ** Ok. Create a new mirror list and allocate a string buffer...
- **/
-
- newList = (URLHMirrorListPtr) NewPtrClear (sizeof (URLHMirrorList));
- theErr = MemError ();
- if (theErr == noErr && newList == 0)
- theErr = memFullErr;
- nrequire (theErr, FailedToAllocateNewList);
-
- theErr = NewMemoryBuffer (&(newList->stringBuffer), STRING_BUFFER_SIZE, STRING_BUFFER_SIZE);
- nrequire (theErr, FailedToAllocateStringBuffer);
-
- /**
- ** Save the mirror list name...
- **/
-
- theErr = MBSaveString (newList->stringBuffer, (char *) &(string[1]), string[0],
- &(newList->oName));
- nrequire (theErr, FailedToSaveListName);
-
- /**
- ** set the rest of the flags...
- **/
-
- newList -> listStatus = kMLModified;
-
- /**
- ** Finally, add the damm thing to the master list
- **/
-
- newList -> next = (*globals)->mirrorList;
- (*globals)->mirrorList = newList;
-
- /**
- ** Done!
- **/
-
- *ref = newList;
- return noErr;
-
- /**
- ** Errors
- **/
-
- FailedToSaveListName: // MB couldn't save the name!
-
- DisposeMemoryBuffer (newList->stringBuffer);
- newList -> stringBuffer = 0;
-
- FailedToAllocateStringBuffer: // Error getting the string buffer up and going!
-
- DisposPtr ((Ptr) newList);
-
- FailedToAllocateNewList: // Couldn't allocate new struct!!!!
-
- BadParams:
-
- *ref = 0;
- return theErr;
- }
-
- /**
- * _URLHGetMirrorList
- *
- * Find (if we can) a named mirror list. When we compare mirror names we ignore
- * case and diatrical marks.
- *
- **/
- pascal ComponentResult _URLHelperGetMirrorList (Handle storage,
- StringPtr string,
- URLHMirrorListPtr *ref)
- {
- OSErr theErr;
- URLGlobalsHandle globals = (URLGlobalsHandle) storage;
- URLHMirrorListPtr p;
- Str255 mName;
-
- /**
- ** Check the params...
- **/
-
- theErr = paramErr;
- require (string != 0 && ref != 0, BadParams);
-
- /**
- ** Loop through the list of mirrors, and find a match
- **/
-
- p = (*globals)->mirrorList;
- while (p) {
-
- theErr = MBGetPString (p->stringBuffer, p->oName, mName);
- nrequire (theErr, FailedToGetMirrorName);
-
- if (EqualString (string, mName, false, false))
- break;
-
- p = p->next;
- }
-
- /**
- ** Ok -- done...
- **/
-
- *ref = p;
- if (p == 0)
- theErr = errURLHNoSuchMirrorList;
-
- return theErr;
-
- /**
- ** Errors
- **/
- FailedToGetMirrorName:
-
- BadParams:
-
- *ref = 0;
- return theErr;
- }
-
- /**
- * _URLHelperAddMirror
- *
- * Add a mirror to a given mirror list.
- *
- **/
- pascal ComponentResult _URLHelperAddMirror (Handle storage,
- URLHMirrorListPtr ref,
- StringPtr mirrorName,
- URLHMirrorFlags flags)
- {
- OSErr theErr;
- URLGlobalsHandle globals = (URLGlobalsHandle) storage;
- URLHMirrorPtr theMirror;
-
- /**
- ** Make sure the params are ok!
- **/
-
- theErr = paramErr;
- require (ref != 0 && mirrorName != 0, BadParams);
-
- /**
- ** Allocate the struct for the new mirror. Put the mirror string name
- ** (with wildcards!) into the string storage for this mirror!
- **/
-
- theMirror = (URLHMirrorPtr) NewPtrClear (sizeof(URLHMirror));
- theErr = MemError ();
- if (theErr == noErr && theMirror == 0)
- theErr = memFullErr;
- nrequire (theErr, FailedToAllocateMirror);
-
- theErr = MBSaveString (ref->stringBuffer, (char *) &(mirrorName[1]), mirrorName[0],
- &(theMirror->oName));
- nrequire (theErr, FailedToSaveName);
-
- theMirror->mirrorFlags = flags;
-
- /**
- ** Add this mirror to the mirror list!
- **/
-
- theMirror->next = ref->firstMirror;
- ref->firstMirror = theMirror;
- ref->listStatus |= kMLModified;
- return noErr;
-
- /**
- ** Errors
- **/
-
- FailedToSaveName: // Name would not be saved!
-
- DisposPtr ((Ptr) theMirror);
-
- FailedToAllocateMirror: // Couldn't allocate space for mirror struct! :(
-
- BadParams:
-
- return theErr;
- }
-
- /**
- ** _URLHelperNumberOfMirrors
- **
- ** For a given mirror list, this will return the number of usable mirrors
- ** in the list.
- **/
- pascal ComponentResult _URLHelperNumberOfMirrors (Handle storage,
- URLHMirrorListPtr ref,
- short *num)
- {
- OSErr theErr;
- URLGlobalsHandle globals = (URLGlobalsHandle) storage;
- URLHMirrorPtr p;
- short total;
-
- /**
- ** Check the params
- **/
-
- theErr = paramErr;
- require (ref != 0 && num != 0, BadParams);
-
- /**
- ** Ok. Loop over the list, counting.
- **/
- total = 0;
- p = ref->firstMirror;
- while (p) {
-
- if (!(p -> mirrorFlags & kURLHMNoUse))
- total++;
-
- p = p -> next;
- }
-
- *num = total;
-
- return noErr;
-
- /**
- ** Errors
- **/
-
- BadParams:
-
- *num = 0;
- return theErr;
- }
-
- /**
- * _URLHelperGetUsableMirror
- *
- * Loop through the mirrors around, and return one that can be used for subst.
- *
- **/
- pascal ComponentResult _URLHelperGetUsableMirror (Handle storage,
- URLHMirrorListPtr mirrorListRef,
- URLHMirrorPtr *mirrorRef,
- short index)
- {
- OSErr theErr;
- URLHMirrorPtr aMirror;
-
- /**
- ** Check incomming params...
- **/
-
- theErr = paramErr;
- require (mirrorListRef != 0 && mirrorRef != 0 && index > 0, BadParams);
- theErr = noErr;
-
- /**
- ** Loop over the list. Ignroe ones that have the nouse flag set...
- **/
-
- aMirror = mirrorListRef -> firstMirror;
- while (aMirror != 0) {
- if (!(aMirror->mirrorFlags & kURLHMNoUse))
- index--;
- if (index == 0)
- break;
- aMirror = aMirror -> next;
- }
-
- if (aMirror == 0)
- theErr = errURLHNoUsableMirror;
- nrequire (theErr, FailedToFindUsableMirror);
-
- *mirrorRef = aMirror;
-
- return noErr;
-
- /**
- ** Handle errors
- **/
-
- FailedToFindUsableMirror:
-
- BadParams:
- *mirrorRef = 0;
-
- return theErr;
- }